<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="省" prop="province">
        <el-select
          v-model="queryParams.province"
          placeholder="请选择省份"
          style="width: 240px"
          @click.native="provincChange"
        >
          <el-option v-for="item in province" :key="item.id" :label="item.province" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="市" prop="city">
        <el-select v-model="queryParams.city" placeholder="请选择城市" clearable style="width: 240px">
          <el-option v-for="dict in cityVal" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
<!--        <el-select-->
<!--          v-model="queryParams.city"-->
<!--          placeholder="请选择城市"-->
<!--          clearable-->
<!--          style="width: 240px"-->
<!--        >-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.sys_city"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
      </el-form-item>
      <el-form-item label="累计确诊" prop="confirm">
        <el-input
          v-model="queryParams.confirm"
          placeholder="请输入累计确诊"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="新增确诊" prop="confirmAdd">
        <el-input
          v-model="queryParams.confirmAdd"
          placeholder="请输入新增确诊"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="累计治愈" prop="heal">
        <el-input
          v-model="queryParams.heal"
          placeholder="请输入累计治愈"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="累计死亡" prop="dead">
        <el-input
          v-model="queryParams.dead"
          placeholder="请输入累计死亡"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="境外输入" prop="overseas">
        <el-input
          v-model="queryParams.overseas"
          placeholder="请输入境外输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['epidemic:details:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['epidemic:details:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['epidemic:details:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['epidemic:details:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleImport"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="累计确诊" align="center" prop="confirm" />
      <el-table-column label="新增确诊" align="center" prop="confirmAdd" />
      <el-table-column label="累计治愈" align="center" prop="heal" />
      <el-table-column label="累计死亡" align="center" prop="dead" />
      <el-table-column label="境外输入" align="center" prop="overseas" />
      <el-table-column label="无症状" align="center" prop="asymptomatic" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['epidemic:details:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['epidemic:details:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改省市确诊数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="省" prop="province">
          <el-select
            v-model="form.province"
            placeholder="请选择省份"
            style="width: 240px"
            @change="provincChange"
          >
            <el-option v-for="item in province" :key="item.id" :label="item.province" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-select v-model="form.city" placeholder="请选择城市" clearable style="width: 240px">
            <el-option v-for="dict in cityVal" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="累计确诊" prop="confirm">
          <el-input v-model="form.confirm" placeholder="请输入累计确诊" />
        </el-form-item>
        <el-form-item label="新增确诊" prop="confirmAdd">
          <el-input v-model="form.confirmAdd" placeholder="请输入新增确诊" />
        </el-form-item>
        <el-form-item label="累计治愈" prop="heal">
          <el-input v-model="form.heal" placeholder="请输入累计治愈" />
        </el-form-item>
        <el-form-item label="累计死亡" prop="dead">
          <el-input v-model="form.dead" placeholder="请输入累计死亡" />
        </el-form-item>
        <el-form-item label="境外输入" prop="overseas">
          <el-input v-model="form.overseas" placeholder="请输入境外输入" />
        </el-form-item>
        <el-form-item label="无症状" prop="overseas">
          <el-input v-model="form.asymptomatic" placeholder="请输入无症状" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetails, getDetails, delDetails, addDetails, updateDetails } from "@/api/epidemic/details";
import { listProvince, getProvince } from '@/api/epidemic/province';
import { cityList } from '@/api/echarts'
export default {
  name: "Details",
  dicts: ['sys_city','sys_province'],
  data() {
    return {
      // 遮罩层
      loading: true,
      cityVal: [],
      province: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 省市确诊数据表格数据
      detailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        province: null,
        city: null,
        confirm: null,
        confirmAdd: null,
        heal: null,
        dead: null,
        overseas: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      upload: {
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/web/details/excel"
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询省市确诊数据列表 */
    getList() {
      this.loading = true;
      listDetails(this.queryParams).then(response => {
        this.detailsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listProvince().then(response => {
        this.province = response.rows
      })
    },
    provincChange(item) {
      console.log('item', item)
      this.cityVal = []
      this.queryParams.city = ''
      cityList({ provinceId: item }).then(res => {
        console.log('ressss', res)
        if (res.code == 200) {
          res.data.map(item => {
            this.cityVal.push({
              name: item.id,
              value: item.city
            })
          })
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        updateTime: null,
        province: null,
        city: null,
        confirm: null,
        confirmAdd: null,
        heal: null,
        dead: null,
        overseas: null,
        asymptomatic: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加省市确诊数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDetails(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改省市确诊数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDetails(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetails(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除省市确诊数据编号为"' + ids + '"的数据项？').then(function() {
        return delDetails(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('epidemic/details/export', {
        ...this.queryParams
      }, `details_${new Date().getTime()}.xlsx`)
    },
    handleImport(){
        this.upload.title = "数据导入";
        this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
